package com.qxsx.midi.activity.personal;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.activity.main.MainActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.CircleImageView;
import com.qxsx.midi.widget.wheelView.addressView.AddrView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by LYJ on 2015/9/8.
 */
public class PersonalDetailsActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout rl_mess, rl_name, rl_email, rl_position, rl_password, rl_phonenum;
    private LinearLayout ll_back;
    TextView tv_name, tv_id;
    CircleImageView img_head;
    Intent intent = new Intent();
    String userid;
    String midi_id, nickname, real_name, eamil, phone, id_card, nationality, birthday, people, head_img;
    int sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_datails);
        setTitle("个人信息");
        setNavBtn(R.mipmap.back, 0);
//        ll_back = (LinearLayout)findViewById(R.id.detail_back);
        rl_mess = (RelativeLayout) findViewById(R.id.detail_mess);
        rl_name = (RelativeLayout) findViewById(R.id.detail_name);
        rl_email = (RelativeLayout) findViewById(R.id.detail_email);
        rl_position = (RelativeLayout) findViewById(R.id.detail_position);
        rl_password = (RelativeLayout) findViewById(R.id.detail_password);
        rl_phonenum = (RelativeLayout) findViewById(R.id.detail_phonenum);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_id = (TextView) findViewById(R.id.tv_id);
        img_head = (CircleImageView) findViewById(R.id.detail_head);
//        ll_back.setOnClickListener(this);
        rl_mess.setOnClickListener(this);
        rl_name.setOnClickListener(this);
        rl_email.setOnClickListener(this);
        rl_position.setOnClickListener(this);
        rl_password.setOnClickListener(this);
        rl_phonenum.setOnClickListener(this);

        userid = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        // OnAccess(userid);

    }


    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;

        }
    }

    void OnAccess(String ID) {
        if (ID == "") {
            Log.e("id为空", ID);
            return;
        }
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        try {
            object.put("user_id", ID);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("109", array, new callBackJsonArray() {
                    @Override
                    public void run(Object code, Object message, JSONArray array) {
                        //  Log.e("-------------", code.toString());
                        if (!code.toString().equals("200")) {
                            Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                        } else if (array == null) {
                            Log.w("array", array + "");
                            return;
                        } else {
                            try {
                                JSONObject jsonObject = array.getJSONObject(0);
                                midi_id = jsonObject.getString("midi_id");
                                nickname = jsonObject.getString("nickname");
                                real_name = jsonObject.getString("real_name");
                                eamil = jsonObject.getString("eamil");
                                phone = jsonObject.getString("phone");
                                id_card = jsonObject.getString("id_card");
                                nationality = jsonObject.getString("nationality");
                                birthday = jsonObject.getString("birthday");
                                people = jsonObject.getString("people");
                                sex = jsonObject.getInt("sex");
                                head_img = jsonObject.getString("head_img");

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
//                            SPUtils.put(AppContext.getContext(), "user_midi_id", midi_id);
//                            SPUtils.put(AppContext.getContext(), "user_nickname", nickname);
//                            SPUtils.put(AppContext.getContext(), "user_real_name", real_name);
//                            SPUtils.put(AppContext.getContext(), "user_phone", phone);
//                            SPUtils.put(AppContext.getContext(), "user_eamil", eamil);
//                            SPUtils.put(AppContext.getContext(), "user_id_card", id_card);
//                            SPUtils.put(AppContext.getContext(), "user_nationality", nationality);
//                            SPUtils.put(AppContext.getContext(), "user_birthday", birthday);
//                            SPUtils.put(AppContext.getContext(), "user_people", people);
//                            SPUtils.put(AppContext.getContext(), "user_sex", sex);
//                            SPUtils.put(AppContext.getContext(), "user_head_img", head_img);

                            tv_name.setText(nickname);
                            tv_id.setText(midi_id);
                            if (!head_img.equals("") && head_img != null && !head_img.isEmpty()) {
//                                Bitmap bitmap = null;
//                                try {
//                                    bitmap = getBitmap(head_img);
//                                } catch (IOException e) {
//                                    e.printStackTrace();
//                                }
//                                img_head.setImageBitmap(bitmap);
                                MyNet.setNetImage(AppContext.getContext(), img_head, head_img);

                            }

//                            Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();

                        }
                    }
                }

        );

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detail_mess:
                //完善信息
                intent.setClass(AppContext.getContext(), PersonalImproveActivity.class);
                startActivity(intent);
                break;
            case R.id.detail_name:
                //昵称
                intent.setClass(AppContext.getContext(), PersonalEditNickNameActivity.class);
                startActivity(intent);
                break;
            case R.id.detail_email:
                //邮箱
                intent.setClass(AppContext.getContext(), PersonalEditEmailActivity.class);
                startActivity(intent);
                break;
            case R.id.detail_position:
                //所在地
                intent.setClass(AppContext.getContext(), PersonalEditPostionActivity.class);
                startActivity(intent);
                break;
            case R.id.detail_password:
                //修改密码
                intent.setClass(AppContext.getContext(), PersonalEditPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.detail_phonenum:
                //修改手机号码
                intent.setClass(AppContext.getContext(), PersonalEditPhoneNumActivity.class);
                startActivity(intent);
                break;

        }
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    // 像素转 dpi
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    @Override
    protected void onResume() {
        super.onResume();
        OnAccess(userid);
    }

    public Bitmap stringtoBitmap(String string) {
        // 将Base64字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }


    public static Bitmap loadBitmap(String url) {
        // 将url转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            InputStream in = new java.net.URL(url).openStream();
            bitmap = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public static Bitmap getBitmap(String path) throws IOException {

        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() == 200) {
            InputStream inputStream = conn.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            return bitmap;
        }
        return null;
    }

}
